unit uSlownikHandl;

interface



uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Buttons, StdCtrls, listlink, Db, ADODB;

type
  TFAktywniHandlowcy = class(TForm)
    ListBox1: TListBox;
    Label1: TLabel;
    ListBox2: TListBox;
    Label2: TLabel;
    Button1: TButton;
    Button3: TButton;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    qTemp: TADOQuery;
    ListBox3: TListBox;
    Label3: TLabel;
    procedure FormShow(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    BazaDyn : string;
  end;

var
  FAktywniHandlowcy: TFAktywniHandlowcy;

implementation

{$R *.DFM}

uses Wilk;

procedure TFAktywniHandlowcy.FormShow(Sender: TObject);
var
  s : string;
  i : integer;
begin
  SQLOpen(qTemp, 'SELECT Handlowiec FROM MBA..TW_wHandlowiec with (NOLOCK) '+
                 ' WHERE Handlowiec NOT IN (SELECT SUBSTRING(Opis,3,200) FROM '+BazaDyn+'..tw_Slownik with (NOLOCK) WHERE Kod=101) '+
                 ' ORDER BY Handlowiec ');
  ListBox1.Items.Clear;
  while not qTemp.EOF do
  begin
    if Trim(qTemp.Fields[0].AsString)<> '' then
      ListBox1.Items.Add(qTemp.Fields[0].AsString);
    qTemp.Next;
  end;

  SQLOpen(qTemp, ' SELECT Opis FROM '+BazaDyn+'..tw_Slownik with (NOLOCK) WHERE Kod=101 '+
                 ' ORDER BY Opis ');
  ListBox2.Items.Clear;
  while not qTemp.EOF do
  begin
    if Trim(qTemp.Fields[0].AsString)<> '' then
    begin
//      s := qTemp.Fields[0].AsString;
//      i := Pos('-', s);
//      ListBox2.Items.Add(copy(s, 1, 2)+'-'+copy(s,3, Length(s)));
        ListBox2.Items.Add(qTemp.Fields[0].AsString);
    end;
    qTemp.Next;
  end;

  // === REGION ===
  SQLOpen(qTemp, 'SELECT Region FROM TW_wRegion with (NOLOCK) ORDER BY Region ');
  ListBox3.Items.Clear;
  while not qTemp.EOF do
  begin
    if Trim(qTemp.Fields[0].AsString)<> '' then
      ListBox3.Items.Add(trim(qTemp.Fields[0].AsString));
    qTemp.Next;
  end;
end;

procedure TFAktywniHandlowcy.Button1Click(Sender: TObject);
var
  s : string;
begin
  // przenies
  if (ListBox1.ItemIndex>=0) and (ListBox3.ItemIndex>=0) then
  begin
    s := ListBox3.Items[ListBox3.ItemIndex]+'-'+trim(ListBox1.Items[ListBox1.ItemIndex]);
    ListBox2.Items.Add(s);
    ListBox1.Items.Delete(ListBox1.ItemIndex);
  end;
end;

procedure TFAktywniHandlowcy.Button3Click(Sender: TObject);
var
  s : string;
  i : integer;
begin
  if ListBox2.ItemIndex>=0 then
  begin
    s := ListBox2.Items[ListBox2.ItemIndex];
    i := Pos('-', s);
    s := copy(s, i+1, Length(s));
    ListBox2.Items.Delete(ListBox2.ItemIndex);
    ListBox1.Items.Add(s);
  end;
end;

end.
